package com.bgs.travelapp.chat.service.Impl;

import com.bgs.travelapp.chat.entity.ChatRecordEntity;
import com.bgs.travelapp.chat.mapper.ChatRecordMapper;
import com.bgs.travelapp.chat.service.ChatRecordService;
import com.bgs.travelapp.user.entity.UserEntity;
import com.bgs.travelapp.user.service.UserService;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 前端---问答
 * gpd 2022-08-15 20点06分
 */
@Service("ChatRecordService")
public class ChatRecordServiceImpl implements ChatRecordService {

    @Resource
    ChatRecordMapper chatRecordMapper;

    @Resource
    ChatRecordService chatRecordService;

    @Resource
    UserService userService;


    /**
     * \
     * 前端聊天发送消息时，将发送人，聊天内容，接收人存进数据库
     * 如果查询数据库有这条聊天记录则修改数据库消息状态
     * 前端---问答
     * gpd 2022-08-15 20点11分
     *
     * @param fromUsernmae
     * @param text
     * @param toUsername
     * @return
     */
    @Override
    public Integer insertChatRecord(String fromUsernmae, String text, String toUsername, String state) {
        ChatRecordEntity chatRecordEntity = new ChatRecordEntity();
        chatRecordEntity.setFromUsername(fromUsernmae);
        chatRecordEntity.setChatContent(text);
        chatRecordEntity.setToUsername(toUsername);
        chatRecordEntity.setChatTime(new Date());
        if (Integer.parseInt(state) == 1) {
            chatRecordEntity.setMessageStatus(1);
        } else {
            chatRecordEntity.setMessageStatus(0);
        }
        return chatRecordMapper.insertSelective(chatRecordEntity);
    }

    /**
     * 查询用户与客服的聊天记录
     * 如果是客服查询则将与用户的消息变为已读
     * <p>
     * 查询用户用户信息（getUserAvatarId方法）
     * 用于聊天显示图片
     * 前端---问答
     * gpd 2022-08-16 09点45分
     *
     * @param fromUserName
     * @param toUserName
     * @return
     */
    @Override
    public Map<String, Object> getUserChatRecord(String fromUserName, String toUserName) {

        if (fromUserName.equals("qwe")) {
            Example example = new Example(ChatRecordEntity.class);
            example.createCriteria().andEqualTo("fromUsername", fromUserName).
                    andEqualTo("toUsername", toUserName).orEqualTo("fromUsername", toUserName).andEqualTo("toUsername", fromUserName);
            int i = 0;
            i = chatRecordService.modifyMessagestate(fromUserName, toUserName);
            List<ChatRecordEntity> chatRecordEntities = chatRecordMapper.selectByExample(example);
            List<UserEntity> userAvatarId = userService.getUserAvatarId(toUserName);
            Map<String, Object> map = new HashMap<>();
            map.put("record", chatRecordEntities);
            map.put("avatarId", userAvatarId);
            return map;
        } else {
            Example example = new Example(ChatRecordEntity.class);
            example.createCriteria().andEqualTo("fromUsername", fromUserName).
                    andEqualTo("toUsername", toUserName).orEqualTo("fromUsername", toUserName).andEqualTo("toUsername", fromUserName);
            List<ChatRecordEntity> chatRecordEntities = chatRecordMapper.selectByExample(example);
            List<UserEntity> userAvatarId = userService.getUserAvatarId(toUserName);
            Map<String, Object> map = new HashMap<>();
            map.put("record", chatRecordEntities);
            map.put("avatarId", userAvatarId);
            return map;
        }

    }

    /**
     * 修改用户消息状态
     * 当客服查看用户发送的未读消息时，将未读消息改为以读消息
     * 前端---问答
     * gpd 2022-08-16 14点07分
     *
     * @param fromUserName
     * @param toUserName
     * @return
     */
    @Override
    public Integer modifyMessagestate(String fromUserName, String toUserName) {
        List<ChatRecordEntity> chatRecordEntities = chatRecordService.getUserMessageStatus(fromUserName, toUserName);
        for (ChatRecordEntity chatRecordEntity : chatRecordEntities) {
            if (chatRecordEntity.getMessageStatus() == 0) {
                chatRecordEntity.setMessageStatus(1);
                chatRecordMapper.updateByPrimaryKeySelective(chatRecordEntity);
            }
        }
        return 1;
    }

    /**
     * 查询用户发送的未读消息和状态
     * 用于修改用户消息状态方法
     * 前端---问答
     * gpd 2022-08-16 15点08分
     *
     * @param fromUserName
     * @param toUserName
     * @return
     */
    @Override
    public List<ChatRecordEntity> getUserMessageStatus(String fromUserName, String toUserName) {
        ChatRecordEntity chatRecordEntity = new ChatRecordEntity();
        chatRecordEntity.setFromUsername(toUserName);
        chatRecordEntity.setToUsername(fromUserName);
        return chatRecordMapper.select(chatRecordEntity);
    }

}
